package com.ms.game.sendgift.db;

import java.util.ArrayList;

import com.ms.game.itembox.db.ItemDAO;
import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.sendgift.message.SendGiftNotifyMessage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class SaveGiftNotifyFinalSendAsync extends TaskAsyncCommand {
	/**
	 * 帳單ID
	 */
	public long orderID;
	
	/**
	 * 訂單資料
	 */
	public SendGiftNotifyModel model;
	
	/**
	 * 寄送訂單中的道具資料
	 */
	public ArrayList<PlayerItemModel> sendGiftItems;
	
	public GUID sessionID;
	
	public String playerName;
	
	public String playerIP;
	
	public Player targetData;
	
	public GUID targetGUID;
	
	public String targetIP;
	
	public SendGiftResponse response;
	
	public GameLobbyModule module;
	
	private SendGiftRecordLog logModel;
	
	private long createTime;
	
	
	public boolean answer;
	
	private boolean isSaveDone = false;

	@Override
	public void asyncExecute() throws Exception {
		if(SendGiftDAO.saveOrUpdateGiftNotify(model)){
			isSaveDone = true;
		}
		
		if(targetData == null && !answer && isSaveDone){
			
			targetData = PlayerDAO.loadPlayerByGuid(targetGUID);
			ItemDAO.LoadItemDataToPlayer(targetData, getCurrentModuleServer());
		}

		//log 記錄
		if(isSaveDone){
			logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setTargetIP(playerIP);
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			if(answer){
				logModel.setCompleteTime(model.getFinishTime());
			}else{
				logModel.setCompleteTime(model.getAnswerTime());
			}

			logModel.setOverTime(model.getOverTime());
			logModel.setSuccess(false);
			logModel.setFinish(model.isFinish());
			logModel.setTaken(model.isTaken());	
			
			createTime = model.getCreateTime().getTime();
			
			if(answer){
				//同意的話就直接記錄
				SendGiftRecordDAO.saveOrUpdateGiftNotify(logModel);
			}
		}
	}

	@Override
	public void execute() throws Exception {
		if (!isSaveDone) {
			// 記錄失敗
			response.errorCode = ErrorCode.SEND_GIFT_SAVE_ERROR;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GIFT_SECOND_ACCEPT_RESPONSE, response);
			return;
		}

		response.createTime = createTime;
		response.errorCode = ErrorCode.SUCCESS;
		response.orderID = model.getSendOrderID();
		MessageAction.sendMessage(sessionID, MessageTypes.S_GIFT_SECOND_ACCEPT_RESPONSE, response);

		// 回覆對方
		if (answer) {
			GUID targetSession = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(targetGUID);
			if (targetSession != null) {
				// 如果對方在線上
				SendGiftNotifyMessage notify = new SendGiftNotifyMessage();
				notify.answer = response.answer;
				notify.fromName = playerName;
				notify.orderID = model.getSendOrderID();
				notify.orderNumber = model.getOrderNumber();
				notify.createTime = createTime;
				MessageAction.sendMessage(targetSession, MessageTypes.S_GIFT_FINIAL_GIVE_RESPONSE, notify);
			}
		}
	}
}
